package trunk.component;

import javax.swing.*;
import java.awt.*;

/**
 * line with a start and a end point
 * Created by IntelliJ IDEA.
 * User: yipeipei
 * Date: 11/28/11
 * Time: 10:54 PM
 * To change this template use File | Settings | File Templates.
 */
public class Line extends JComponent implements StatusAlterable {
    protected int xStart;
    protected int yStart;
    protected int xEnd;
    protected int yEnd;
    protected Status status = Status.NORMAL;

    public Line(int xStart, int yStart, int xEnd, int yEnd, Status status) {
        this.xStart = xStart;
        this.yStart = yStart;
        this.xEnd = xEnd;
        this.yEnd = yEnd;
        this.status = status;
    }

    public Line(int xStart, int yStart, int xEnd, int yEnd) {
        this.xStart = xStart;
        this.yStart = yStart;
        this.xEnd = xEnd;
        this.yEnd = yEnd;
    }

    public Line(int xStart, int yStart, Direction direction, int length) {
        int xEnd;
        int yEnd;

        switch (direction) {
            case UP:
                xEnd = xStart;
                yEnd = yStart - length;
                break;
            case DOWN:
                xEnd = xStart;
                yEnd = yStart + length;
                break;
            case RIGHT:
                xEnd = xStart + length;
                yEnd = yStart;
                break;
            case LEFT:
                xEnd = xStart - length;
                yEnd = yStart;
                break;
            default:
                xEnd = 0;
                yEnd = 0;
        }

        this.setxStart(xStart);
        this.setyStart(yStart);
        this.setxEnd(xEnd);
        this.setyEnd(yEnd);
    }

    @Override
    public void paint(Graphics g) {
        //set color
        if (this.status == Status.NORMAL) {
            g.setColor(Color.black);
        } else if (this.status == Status.ACTIVE) {
            g.setColor(Color.red);
        }
        //paint
        g.drawLine(xStart, yStart, xEnd, yEnd);
        //restore color
        g.setColor(Color.black);
    }

    public int getxStart() {
        return xStart;
    }

    public void setxStart(int xStart) {
        this.xStart = xStart;
    }

    public int getyStart() {
        return yStart;
    }

    public void setyStart(int yStart) {
        this.yStart = yStart;
    }

    public int getxEnd() {
        return xEnd;
    }

    public void setxEnd(int xEnd) {
        this.xEnd = xEnd;
    }

    public int getyEnd() {
        return yEnd;
    }

    public void setyEnd(int yEnd) {
        this.yEnd = yEnd;
    }

    public Status getStatus() {
        return status;
    }

    public void setStatus(Status status) {
        this.status = status;
    }
}
